/* eslint-disable no-undef */
console.log('invoice injected');
import './../../lib/documentReady.js';
import { E$ } from './../../lib/easyJQ.js';

let inputData;

document.ready(function () {
  if (/\/sword/g.test(window.location.pathname)) {
    window.postMessage({
      event: 'getInvoiceInfo'
    }, '*');
  }
});

window.addEventListener('message', function ({ data }) {
  // 只监听来自于content script的信息
  if (data.sender !== 'content-script') {
    return false;
  }
  if (data.event === 'getInvoiceInfo') {
    data = data.data;
    inputData = data;
    afterGetInvoiceInfo();
  }
}, false);

const afterGetInvoiceInfo = () => {
  const search = window.location.search;
  if (search.includes('syInitView')) {
    confirmTips();
    addUIControl();
  } else if (search.includes('sqNext')) {
    document.querySelector('body > table > tbody > tr > td:nth-child(3) > div').click();
  } else if (search.includes('nextLqfs')) {
    confirmTips4Mail();
    autoInput4Mail();
  }
};

const addUIControl = () => {
  const { salesAmountIncludingTax = '' } = inputData.goodsInfo || {};
  let appendHtml = `<div style="position: fixed; top: 200px; left: 0;background-color: #49a2e8;height: 160px;width: 250px;display: flex;flex-direction: column;justify-content: center;align-items: center;color:#49a2e8;font-size: 16px">
      <p style="color: #fff; margin: 0; font-weight: bold; width: 160px;">1.请先选择业务发生地</p>
      <p style="margin: 0; color: #f1f1f1">业务发生地： <span style="color:yellow">渝北区</span></p>
      <p style="color: #fff; margin: 0; font-weight: bold; width: 160px;">2.请点击自动填写按钮</p>
      <button class="J-auto-input" style="border: 0;border-radius: 2px;background-color: yellow; color: grey;">自动填写基础信息</button>
      <p style="color: #fff; margin: 0; font-weight: bold; width: 160px;">3.请填写含税销售额</p>`;
  if (salesAmountIncludingTax) {
    appendHtml += `<p style="margin: 0; color: #f1f1f1">含税销售额：<span style="color:yellow">${salesAmountIncludingTax}</span></p>
     </div>`;
  } else {
    appendHtml += '</div>';
  }
  jQuery('body').append(appendHtml);
  jQuery('.J-auto-input').click(autoInput);
};

const autoInput = () => {
  if (!Object.keys(inputData).length) {
    alert('请从会计平台化进入，可快捷填报！');
    return;
  }
  const data = inputData;

  const $ = jQuery;
  const hwlw = {
    caption: '设计/技术服务/劳务费',
    code: '2017041495791905440',
    gjc: '系统设计及开发',
    gsmsbz: '',
    hwlwmc: '设计/技术服务/劳务费(信息系统集成服务)',
    hymc: '信息系统集成服务',
    hymxDm: '6531',
    msbz: 'N',
    zspmDm: '101016201',
    zycbkcbz: ''
  };
  // 业务发生地
  $('#dksbdForm_xzqhszDm').attr('originvalue', data.payeeInfo.area).attr('realvalue', '500112').val(data.payeeInfo.area);
  $('#dksbdForm_jdxzDm').attr('realvalue', '500112005').attr('code', '500112005').val('龙山街道');
  $('#fkfjbxxForm_fkfnsrsbh').attr('realvalue', data.payerInfo.taxNo).val(data.payerInfo.taxNo);
  $('#fkfjbxxForm_fkfmc').attr('realvalue', data.payerInfo.taxName).val(data.payerInfo.taxName);
  $w('dksbdForm').setValue('zgswskfjDm', '15001121000');
  $('#dksbdForm_zgswskfjDm').val('国家税务总局重庆市渝北区税务局第一税务所');
  $('#dksbdForm_zgswskfjDm').attr('realvalue', '15001121000').attr('code', '15001121000').val('国家税务总局重庆市渝北区税务局第一税务所');
  $('#hwlwmcid').attr('realvalue', 'code,2017041495791905440|caption,设计/技术服务/劳务费').attr('code', '2017041495791905440');
  $('#kxzspmForm_hwlwmc').attr('realvalue', 'code,2017041495791905440|caption,设计/技术服务/劳务费').attr('code', '2017041495791905440').val('设计/技术服务/劳务费');
  window.onChange_hwlwmc(null, hwlw);
  $(".sGrid_data_div div[name='hwlwmc']").attr('realvalue', data.goodsInfo.serviceName).attr('showValue', data.goodsInfo.serviceName).text(data.goodsInfo.serviceName);
  // $(".sGrid_data_div div[name='xse']").attr("realvalue", data.goodsInfo.salesAmountIncludingTax).attr('showValue', data.goodsInfo.salesAmountIncludingTax).attr('title', data.goodsInfo.salesAmountIncludingTax).text(data.goodsInfo.salesAmountIncludingTax).trigger('blur');
  console.log(data);
};

/**
 * 自动点击已阅读弹窗
 */
const confirmTips = function () {
  const iframes = document.getElementsByTagName('iframe') || [];
  const maskIFrame = [...iframes].filter(item => item.name.includes('IFrame'))[0];
  maskIFrame.contentWindow.document.getElementsByName('jsBtn')[0].click();
};

const confirmTips4Mail = () => {
  document.querySelector("#fplqfs_lqfs > div > div:nth-child(1) > input[type='radio']").click();
  setTimeout(confirmTips, 500);
};

const autoInput4Mail = () => {
  if (!Object.keys(inputData).length) {
    alert('请从会计平台化进入，可快捷填报！');
    return;
  }
  const data = inputData;

  // 业务发生地
  E$('#yjfp_lqrxm').attr('value', data.mailInfo.invoicePeople).attr('ovalue', data.mailInfo.invoicePeople).val(data.mailInfo.invoicePeople);
  E$('#yjfp_lxdh').attr('value', data.mailInfo.invoicePhone).attr('ovalue', data.mailInfo.invoicePhone).val(data.mailInfo.invoicePhone);
  E$('#yjfp_xxdz').attr('value', data.mailInfo.invoiceAddr).attr('ovalue', data.mailInfo.invoiceAddr).val(data.mailInfo.invoiceAddr);
  E$('#yjfp_xzqh').attr('originvalue', data.payeeInfo.area).attr('realvalue', '500112').val(data.payeeInfo.area);
  console.log(data);
};
